Skip to content

Update TaskCompletionSource constructor to use TaskCreationOptions.RunContinuationsAsynchronously#400

Closed
salgatbp wants to merge 1 commit intokurrent-io:masterfrom
salgatbp:taskcompletionsource_options
Closed

Update TaskCompletionSource constructor to use TaskCreationOptions.RunContinuationsAsynchronously#400
salgatbp wants to merge 1 commit intokurrent-io:masterfrom
salgatbp:taskcompletionsource_options

Conversation

@salgatbp
Copy link
Copy Markdown
Contributor

This is to avoid potential deadlocks due to synchronous continuations. More specifically, we're seeing a potential deadlock scenario in the KurrentDB client and are trying to narrow down potential causes.

Copilot AI review requested due to automatic review settings March 18, 2026 15:02
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates internal TaskCompletionSource<T> instantiations to use TaskCreationOptions.RunContinuationsAsynchronously to reduce the risk of deadlocks caused by inline/synchronous continuations in the KurrentDB client’s streaming read/append flows.

Changes:

  • Use RunContinuationsAsynchronously for the read operation’s ReadState completion source.
  • Use RunContinuationsAsynchronously for the batch append usability signal and per-request completion sources.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/KurrentDB.Client/Streams/KurrentDBClient.Read.cs Updates the ReadState TaskCompletionSource to run continuations asynchronously.
src/KurrentDB.Client/Streams/KurrentDBClient.Append.cs Updates batch append TaskCompletionSource instances to run continuations asynchronously.
Comments suppressed due to low confidence (1)

src/KurrentDB.Client/Streams/KurrentDBClient.Append.cs:249

  • complete is added to _pendingRequests before any writes to the channel. If _channel.Writer.WriteAsync(...) later throws ChannelClosedException (or any other exception in this try block), this entry is never removed, which can leak _pendingRequests entries on failure paths. Consider ensuring the correlationId is removed in the exception path (or only add the TCS after the request is successfully queued).
					var complete = _pendingRequests.GetOrAdd(correlationId, new TaskCompletionSource<IWriteResult>(TaskCreationOptions.RunContinuationsAsynchronously));

					try {
						foreach (var appendRequest in GetRequests(events, options, correlationId))
							await _channel.Writer.WriteAsync(appendRequest, cancellationToken).ConfigureAwait(false);

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

var correlationId = Uuid.NewUuid();

var complete = _pendingRequests.GetOrAdd(correlationId, new TaskCompletionSource<IWriteResult>());
var complete = _pendingRequests.GetOrAdd(correlationId, new TaskCompletionSource<IWriteResult>(TaskCreationOptions.RunContinuationsAsynchronously));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not relevant to this PR.

@w1am
Copy link
Copy Markdown
Contributor

w1am commented Mar 23, 2026

Hey @salgatbp. Thanks for submitting the PR, I cherry-picked your change into #402 because the tests currently don't run when created by outside collaborators.

@salgatbp
Copy link
Copy Markdown
Contributor Author

Hey @salgatbp. Thanks for submitting the PR, I cherry-picked your change into #402 because the tests currently don't run when created by outside collaborators.

Sounds good, I'll close this PR.

@salgatbp salgatbp closed this Mar 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants